package com.web4code.codegen.utils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;

import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

/**
 * Git工具类
 * 
 * @author web4code
 */
public class GitUtils {
    public static Git gitClone(String gitUrl, String localPath) throws Exception {
        return gitClone(gitUrl, localPath, null, null);
    }

    public static Git gitClone(String gitUrl, String localPath, String branch) throws Exception {
        return gitClone(gitUrl, localPath, branch, null);
    }

    public static Git gitClone(String gitUrl, String localPath, String branch, CredentialsProvider credentialsProvider) throws Exception {
        CloneCommand cloneCommand = Git.cloneRepository()
            .setURI(gitUrl)
            .setDirectory(new File(localPath))
        ;
        if (branch != null) {
            cloneCommand.setBranch(branch);
        }
        if (credentialsProvider != null) {
            cloneCommand.setCredentialsProvider(credentialsProvider);
        }
        Git git = cloneCommand.call();
        return git;
    }

    public static CredentialsProvider createCredential(String userName, String password) {
        return new UsernamePasswordCredentialsProvider(userName, password);
    }

    public static void gitCommit(Git git, String message, CredentialsProvider provider) throws GitAPIException {
        git.add().addFilepattern(".").call();
        git.commit()
            .setMessage(message)
            .call();
    }

    public static void gitPull(Repository repo, CredentialsProvider provider) throws GitAPIException {
        Git git = new Git(repo);
        gitPull(git, provider);
    }

    public static void gitPull(Git git, CredentialsProvider provider) throws GitAPIException {
        git.pull()
            .setCredentialsProvider(provider)
            .call();
    }

    public static void gitPush(Git git, CredentialsProvider provider) throws GitAPIException, IOException {
        gitPush(git,null,provider);
    }

    public static void gitPush(Git git, String branch, CredentialsProvider provider) throws GitAPIException, IOException {
        if (branch == null) {
            branch = git.getRepository().getBranch();
        }
        git.push()
            .setCredentialsProvider(provider)
            .setRemote("origin").setRefSpecs(new RefSpec(branch))
            .call();
    }

    public static Repository getRepositoryFromDir(String dir) throws IOException {
        File gitDir = Paths.get(dir, ".git").toFile();
        if (!gitDir.exists()) {
            return null;
        }
        return new FileRepositoryBuilder()
            .setGitDir(gitDir)
            .build();
    }
}
